#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 28 16:33:53 2018
查看历史上占用最高CPU百分比
和叫一个名字的所有进程占用最高内存
@author: bb
"""
import psutil
import time


def get_total_used_memory(task_name):
    whole=0
    for id in psutil.pids():
        p = psutil.Process(id)
        if (p.name() == task_name):
            mem = p.memory_info()[0] / float(2**20)  #把单位从byte转换为MiB
            whole+=mem
    return whole


record = 0
while True:
    try:
        mem = get_total_used_memory('mps_optim')
        if mem>record:
            record=mem
    except:
        time.sleep(1)
    max_system_memory=0
    system_memory=psutil.virtual_memory().percent
    if system_memory>max_system_memory:
        max_system_memory=system_memory
    print('\n')
    print('Task_memory  Task_max_memory  System_memory  Max_system_memory' )
    print('%.3f MiB    %.3f MiB        %.1f %%         %.1f %% '%(mem,record,system_memory,max_system_memory))
    time.sleep(0.5)


